package com.hxzy.controller.admin.product;

import com.github.pagehelper.PageInfo;
import com.hxzy.common.annotates.RequiredRole;
import com.hxzy.common.constants.MarketConstants;
import com.hxzy.common.controller.BaseController;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.common.resp.R;
import com.hxzy.controller.admin.product.req.*;
import com.hxzy.controller.admin.product.resp.ProductResp;
import com.hxzy.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author admin
 */
@Api(tags = "后台-商品Api")
@RestController
@RequestMapping(value = "/api/product")
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;

    @ApiOperation(value = "查询")
    @GetMapping(value = "/data")
    public R<PageInfo<ProductResp>> search(@Valid ProductSearchReq req){
       super.startPage(req);
        List<ProductResp> list=this.productService.search(req);
        return super.buildPageInfo(list);
    }

    @RequiredRole(role = MarketConstants.ACCOUNT_MERCHANDISER_TYPE)
    @ApiOperation(value = "新增商品")
    @PostMapping
    public R addData(@RequestBody @Valid ProductAddReq req){
        boolean result=this.productService.addData(req);
        return result?R.ok():R.build(AckCode.DATABASE_ERROR);
    }

    @ApiOperation(value = "根据主键查询")
    @GetMapping(value ="/{id}" )
    public R<ProductResp> findById(@PathVariable(value = "id") Long id){
       if(Objects.isNull(id) || id<0L){
           throw new ServiceException(AckCode.SYSTEM_PARAM_FAIL);
       }
       ProductResp resp=this.productService.findById(id);
       return R.okHasData(resp);
    }

    @RequiredRole(role = MarketConstants.ACCOUNT_MERCHANDISER_TYPE)
    @ApiOperation(value = "修改商品")
    @PutMapping
    public R updateData(@RequestBody @Valid ProductEditReq req){
        boolean result=this.productService.updateData(req);
        return result?R.ok():R.build(AckCode.DATABASE_ERROR);
    }


    @RequiredRole(role = MarketConstants.ACCOUNT_MERCHANDISER_TYPE)
    @ApiOperation(value = "单个商品上下架")
    @PutMapping(value = "/urOrDown")
    public R upOrDown(@RequestBody @Valid ProductUpOrDownReq req){
        boolean result=this.productService.upOrDown(req);
        return result ? R.ok() : R.build(AckCode.DATABASE_ERROR);
    }


    @RequiredRole(role = MarketConstants.ACCOUNT_MERCHANDISER_TYPE)
    @ApiOperation(value = "修改库存")
    @PutMapping(value = "/stock/change")
    public R changeStock(@RequestBody @Valid ProductStockChangeReq req){
        boolean result=this.productService.changeStock(req);
        return result ? R.ok() : R.build(AckCode.DATABASE_ERROR);
    }


    @RequiredRole(role = MarketConstants.ACCOUNT_MERCHANDISER_TYPE)
    @ApiOperation(value = "批量下架")
    @PutMapping(value = "/downbatch")
    public R batchDown(Long[] ids){
        if(Objects.isNull(ids) || ids.length==0){
            throw new ServiceException(AckCode.SYSTEM_PARAM_FAIL);
        }
        boolean result=this.productService.batchDown(ids);
        return result ? R.ok() : R.build(AckCode.DATABASE_ERROR);
    }



}
